package com.qf.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.qf.feign.SearchFeign;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/10/10 16:57
 */
@RestController
@RequestMapping("/custom")
public class CuseromerController {
//    spring提供访问rest服务器的
    @Autowired
    private RestTemplate restTemplate;
//    注册中心Eureka
    @Autowired
    private EurekaClient eurekaClient;
//    注入feign
    @Autowired
    private SearchFeign searchFeign;
    @RequestMapping("/hello")
    public String testHello(){
        /*
        *通过被调用的服务器名称,获取服务器信息
        */
        InstanceInfo server = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);
        String url = server.getHomePageUrl();
        System.out.println(server.getIPAddr()+url);
//        调用服务器
//        String str = restTemplate.getForObject(url+"/search/hello", String.class);
        //3. 通过restTemplate访问, 注意这里使用rabbin必须是通过服务名访问, 才可以负载均衡(默认是使用轮询)
        String str = restTemplate.getForObject("http://QF-SEARCH/search/hello", String.class);
        return str;
    }
    @RequestMapping("/testFeign")
    public String testFeignHello(){
        String s = searchFeign.Hello();
        return s;
    }
    @RequestMapping("/findById")
    public User findById(Integer id){

        User user = searchFeign.findUserById(id);
        return user;
    }
    @RequestMapping("/insertUser")
    public String insertUser(@RequestBody User user){
        String s = searchFeign.insertUser(user);
        return s;
    }
}
